package com.flashphoner.app;

import com.flashphoner.sdk.rtmp.Config;
import com.flashphoner.sdk.sip.ISipMessageListener;
import com.flashphoner.sdk.softphone.Logger;
import gov.nist.javax.sip.address.SipUri;
import gov.nist.javax.sip.message.SIPMessage;
import gov.nist.javax.sip.message.SIPRequest;

import javax.sip.header.FromHeader;
import javax.sip.message.Request;
import java.text.ParseException;

/**
 * Created by Flashphoner.com team
 * User: Alexey
 * Date: 04.01.2011
 * Time: 12:49:24
 *
 * @Copyright Flashphoner.com team
 */
public class MySipMessageListener implements ISipMessageListener {
    @Override
    public void processMessage(SIPMessage sipMessage) {       

        String method = sipMessage.getCSeq().getMethod();

        if ((sipMessage instanceof SIPRequest) && (Request.REGISTER.equals(method) || (Request.INVITE.equals(method)))) {
            FromHeader fromHeader = (FromHeader) sipMessage.getHeader(FromHeader.NAME);
            String sipUri = fromHeader.getAddress().getURI().toString();
            String sipUri2 = sipUri.substring(0, sipUri.indexOf("@")) + "@" + Config.getInstance().getProperty("ip");

            try {
                fromHeader.getAddress().setURI(new gov.nist.javax.sip.address.GenericURI(sipUri2));
            } catch (ParseException e) {
                Logger.logger.error(e);
            }

            Logger.logger.info("MySipMessageListener processMessage: mapping " + sipUri + " --> " + sipUri2);
        }

    }
}
